﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Configuration;
using IHISCETeim.Model;
using System.Data.SqlClient;
namespace IHISCETeim.SendSMSSvr
{
    public class DAL
    {
        #region 操作短信列表,smslist
        /// <summary>
        /// 添加短信
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int SMSList_Add(SMSListInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

          //  DbCommand dbCommand = DB.GetStoredProcCommand("SMSList_Add", obj.SMSType, obj.Mobile, obj.SMSContent,obj.SendType, obj.NeedSendDate);

            DbCommand dbCommand = DB.GetStoredProcCommand("SMSList_Add");
            DB.AddInParameter(dbCommand, "SMSType", DbType.String, obj.SMSType);
            DB.AddInParameter(dbCommand, "Mobile", DbType.String, obj.Mobile);//
            DB.AddInParameter(dbCommand, "SMSContent", DbType.String, obj.SMSContent);
            DB.AddInParameter(dbCommand, "SendType", DbType.String, obj.SendType);

            SqlParameter NeedSendDate = new SqlParameter("@NeedSendDate", SqlDbType.DateTime);
            NeedSendDate.IsNullable = true;
            if (obj.NeedSendDate == DateTime.MinValue)
                NeedSendDate.Value = DBNull.Value;
            else
                NeedSendDate.Value = obj.NeedSendDate;

            dbCommand.Parameters.Add(NeedSendDate);

            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        /// <summary>
        /// 删除短信
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static bool SMSList_Delete(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SMSList_Delete", ID);
            int result = Convert.ToInt32(DB.ExecuteScalar(dbCommand));
            return result > 0;
        }
        /// <summary>
        /// 获取需要发送的短信列表,
        /// </summary>
        /// <param name="SMSType">如果为''空表示获取所有</param>
        /// <param name="GetToDay">0表示获取所有的,只要是未发送的.1表示只获取未发送,并是今天的短信</param>
        /// <returns></returns>
        public static DataTable SMSList_GetNeedSendBySMSType(string SMSType, bool GetToDay)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("SMSList_GetNeedSendBySMSType", SMSType, GetToDay);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
	    }
        /// <summary>
        /// 已发送后,更改状态为已发送,更改真实发送日期为当日,有一种情况,如果是定时发送的,那么REALSENDDATE字段的值为推送到短信提供商服务器的日期,不是真实的发送日期
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static bool SMSList_Update(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SMSList_Update", ID);
            try
            {
                return Convert.ToInt32(DB.ExecuteScalar(dbCommand)) > 0;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        #endregion
    }
}
